#include<iostream>
#include<vector>
#include<string>
#include<sw/redis++/redis++.h>
#include<chrono>
#include<thread>
#include"util.hpp"
using namespace sw::redis;

using std::cout;
using std::endl;
using std::vector;
using std::string;

void test1(Redis& redis)
{
    std::cout<<"get set"<<std::endl;
    redis.flushall();

    redis.set("key1","111");
    redis.set("key2","222");
    redis.set("key3","333");
    redis.set("key4","444");
    redis.set("key5","555");

    //get 返回的是optionalstring，为的是区分nil和正常元素 optional可以隐式转换为bool

    auto value1 = redis.get("key1");
    if(value1)
    {
        std::cout<<"value1: "<<value1.value()<<std::endl;
    }

    auto value2 = redis.get("key2");
    if(value2)
    {
        std::cout<<"value2: "<<value2.value()<<std::endl;
    }

    auto value3 = redis.get("key3");
    if(value3)
    {
        std::cout<<"value3: "<<value3.value()<<std::endl;
    }

    auto value4 = redis.get("key4");
    if(value4)
    {
        std::cout<<"value4: "<<value4.value()<<std::endl;
    }

    auto value5 = redis.get("key5");
    if(value5)
    {
        std::cout<<"value5: "<<value5.value()<<std::endl;
    }
}

void test2(Redis& redis)
{
    std::cout<<"Exists"<<std::endl;
    redis.flushall();
    redis.set("key1","111");
    redis.set("key2","222");
    redis.set("key3","333");
    redis.set("key4","444");
    redis.set("key5","555");

    bool ret= redis.exists("key1");
    std::cout<<"key1: "<<ret<<std::endl;
    ret=redis.exists("Key6");
    std::cout<<"key6: "<<ret<<std::endl;

    int count=redis.del("key1");
    ret= redis.exists("key1");
    std::cout<<"key1: "<<ret<<std::endl;
    std::cout<<"del: "<<count<<std::endl;
}

void test3(Redis& redis)
{
    using namespace std::chrono_literals;
    std::cout<<"Expire ttl"<<std::endl;
    redis.flushall();
    redis.set("key1","111");
    redis.set("key2","222");
    redis.set("key3","333");
    redis.set("key4","444");
    redis.set("key5","555");

    redis.expire("key1",10s);

    std::this_thread::sleep_for(2s);
    auto time=redis.ttl("key1");
    std::cout<<"time: "<<time<<std::endl;
}

void test4(Redis& redis)
{
    using namespace std::chrono_literals;
    std::cout<<"keys"<<std::endl;
    redis.flushall();
    redis.set("key1","111");
    redis.set("key2","222");
    redis.set("key3","333");
    redis.set("key4","444");
    redis.set("key5","555");
    
    vector<string> v;
    auto it=std::back_inserter(v);
    redis.keys("*",it);
    printContainer(v);
}

void test6(sw::redis::Redis& redis) {
    std::cout << "type" << std::endl;
    redis.flushall();

    redis.set("key", "111");
    string result = redis.type("key");
    std::cout << "key: " << result << std::endl;

    redis.lpush("key2", "111");
    result = redis.type("key2");
    std::cout << "key2: " << result << std::endl;

    redis.hset("key3", "aaa", "111");
    result = redis.type("key3");
    std::cout << "key3: " << result << std::endl;

    redis.sadd("key4", "aaa");
    result = redis.type("key4");
    std::cout << "key4: " << result << std::endl;

    redis.zadd("key5", "吕布", 99);
    result = redis.type("key5");
    std::cout << "key5: " << result << std::endl;
}
int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    test6(redis);
    return 0;
}

